import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 19182
 * Date: 2025-09-03
 * Time: 19:34
 */
public class SortTest3 {
    public void ShellSort(int[] array){
        int gap = array.length;
        while(gap > 1){
            gap = gap / 2;
            shell(array, gap);
        }
    }
    private void shell(int[] array, int gap){
        for(int i = gap; i < array.length; i++){
            int end = i - gap;
            int tmp = array[i];
            while(end >= 0){
                if(array[end] > tmp){
                    array[end + gap] = array[end];
                    end -= gap;
                }else{
                    break;
                }
            }
            array[end + gap] = tmp;
        }
    }

    public void heapSort(int[] array){
        //建大堆
        for(int i = (array.length - 1 - 1) / 2; i >= 0; i--){
            siftdown(array, i, array.length);
        }
        int end = array.length - 1;
        while(end > 0){
            swap(array, 0, end);
            siftdown(array, 0, end);
            end--;
        }
    }
    private void siftdown(int[] array, int parent, int n){
        int child = 2 * parent + 1;
        while(child < n){
            if(child + 1 < n && array[child + 1] > array[child]){
                child++;
            }
            if(array[parent] < array[child]){
                swap(array, parent, child);
                parent = child;
                child = 2 * parent + 1;
            }else{
                break;
            }
        }
    }
    private void swap(int[] array, int x, int y){
        int tmp = array[x];
        array[x] = array[y];
        array[y] = tmp;
    }
    public void quickSort(int[] array){
        quick(array, 0, array.length - 1);
    }
    public void quick(int[] array, int left, int right){
        if(left >= right){
            return;
        }
        if(right - left + 1 <= 10){
            insertRangeSort(array, left, right);
        }
        int key = getThreeMid(array, left, right);   //取中间值, 防止有序状态下选取中间值每次递归划分不平均分支
        swap(array, left, key);
        int par = partitionHoare(array, left, right);
        quick(array, left, par - 1);
        quick(array, par + 1, right);
    }
    private int partitionHoare(int[] array, int low, int hign){
        int key = array[low];
        int tmp = low;
        while(low < hign){
            while(low < hign && array[hign] >= key){
                hign--;
            }
            while(low < hign && array[low] <= key){
                low++;
            }
            swap(array, low, hign);
        }
        swap(array, tmp, low);
        return low;
    }
    private int partitionHole(int[] array, int low, int hign){
        int key = array[low];
        while(low < hign){
            while(low < hign && array[hign] >= key){
                hign--;
            }
            array[low] = array[hign];
            while(low < hign && array[low] <= key){
                low++;
            }
            array[hign] = array[low];
        }
        array[low] = key;
        return low;
    }
    private int getThreeMid(int[] array, int left, int right){
        int mid = left + (right - left) / 2;
        if(array[left] < array[right]){
            if(array[mid] > array[right]){
                return right;
            }else if(array[mid] > array[left]){
                return mid;
            }else{
                return left;
            }
        }else{
            if(array[mid] < array[right]){
                return right;
            }else if(array[mid] > array[right]){
                return mid;
            }else{
                return left;
            }
        }
    }
    private void insertRangeSort(int[] array, int low, int hign){
        for(int i = low; i <= hign; i++){
            int tmp = array[i + 1];
            int end = i;
            while(end >= low){
                if(array[end] > tmp){
                    array[end + 1] = array[end];
                    end--;
                }else{
                    break;
                }
            }
            array[end + 1] = tmp;
        }
    }
    public void quickSortNor(int[] array){
        int low = 0;
        int hign = array.length - 1;
        int par = partitionHoare(array, low, hign);
        Stack<Integer> st = new Stack<>();
        if(par > low + 1){
            st.push(low);
            st.push(par - 1);
        }
        if(par < hign - 1){
            st.push(par + 1);
            st.push(hign);
        }
        while(!st.empty()){
            hign = st.pop();
            low = st.pop();
            par = partitionHoare(array, low, hign);
            if(par > low + 1){
                st.push(low);
                st.push(par - 1);
            }
            if(par < hign - 1){
                st.push(par + 1);
                st.push(hign);
            }
        }
    }
    public void mergeSort(int[] array){
        mergeSortChild(array, 0, array.length - 1);
    }
    private void mergeSortChild(int[] arary, int left, int right){
        if(left >= right){
            return;
        }
        int mid = left + (right - left) / 2;
        mergeSortChild(arary, left, mid);
        mergeSortChild(arary, mid + 1, right);
        merge(arary, left, mid, right);
    }
    private void merge(int[] array, int left, int mid, int right) {
        int[] tmp = new int[right - left + 1];
        int s1 = left;
        int e1 = mid;
        int s2 = mid + 1;
        int e2 = right;
        int k = 0;
        while(s1 <= e1 && s2 <= e2){
            if(array[s1] < array[s2]){
                tmp[k++] = array[s1];
                s1++;
            }else {
                tmp[k++] = array[s2];
                s2++;
            }
        }
        while(s1 <= e1){
            tmp[k++] = array[s1++];
        }
        while(s2 <= e2){
            tmp[k++] = array[s2++];
        }
        //拷贝回原数组
        for(int i = 0; i < tmp.length; i++){
            array[i + left] = tmp[i];
        }

    }
}
